package com.zsj.juc.reference;


import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/5/19 16:50
 * @description 弱引用
 */
public class WeakReferenceDemo {

    public static void main(String[] args) {
        //弱引用只要一GC就回收
        WeakReference<TestObject> weakReference = new WeakReference<>(new TestObject());
        System.out.println("gc before" + weakReference.get());
        System.gc();
        //等gc执行
        try {TimeUnit.MILLISECONDS.sleep(500);}catch (InterruptedException e){e.printStackTrace();}
        System.out.println("gc after" + weakReference.get());
    }

}
